/**
 *	api目录下的控制器，正常是需要登录
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-19
 */

package com.framework.Controller.api;



import com.framerock.baselib.Jiami;
import com.framerock.server.Response;
import com.framerock.baselib.Json;
import com.framerock.baselib.Patter;
import com.framerock.baselib.Rock;
import com.framerock.baselib.RockMsg;
import com.framework.Flow.Flow;

import java.util.ArrayList;
import java.util.Map;

/**
 * 流程的控制器
 * */
public class ControllerFlow extends ControllerApi  {

    private Flow flow;


    protected String runAction(String action)
    {
        if(action.equals("editcell"))return editcell();
        if(action.equals("input"))return input();
        if(action.equals("inputsave"))return inputsave();
        if(action.equals("loadstore"))return loadstore();
        if(action.equals("optmenu"))return optmenu();
        if(action.equals("optmenurun"))return optmenurun();
        if(action.equals("xiang"))return xiang();
        if(action.equals("delete"))return delete();
        if(action.equals("listinit"))return listinit();
        if(action.equals("checkping"))return checkping();
        if(action.equals("subdata"))return subdata();
        if(action.equals("action"))return action();
        return super.runAction(action);
    }

    /**
     * 初始流程
     * */
    private String initflow()
    {
        String num      = mRequest.get("systemnum");
        if(Rock.isEmpt(num))num = mRequest.get("num");
        if(Rock.isEmpt(num))return Response.returnError("modenum is empty");
        this.flow       = Flow.get(this.nDb, num, mRequest);
        if(mRequest.isError())return mRequest.getError();
        this.flow.initFunit(mRequest.get("funitid"));
        if(mRequest.isError())return mRequest.getError();
        return  "";
    }

    /**
     * 列表页数据
     * */
    public String data()
    {
        String bstr = initflow();if(bstr.length()>0)return bstr;

        int loadci  = Integer.parseInt(mRequest.get("loadci", "1"));
        Json json   = new Json();
        if(loadci==1) {
            json.putVal("fieldsarr", flow.getFieldsArr(2));
            json.putVal("moders", flow.moders);
            json.putVal("store", flow.getallStore(0));
        }

        json.putVal("rows", Json.getListString(flow.getFlowData()));
        json.putInt("totalCount", flow.mDBLimit.getCount());
        json.putVal("modejs", flow.mFlowListener.onModejs() ? "1": "");

        String filedata = Json.getListString(flow.getFiledata(2));
        json.putVal("filedata", filedata);

        Map<String, String> after = flow.mFlowListener.onDataafter();
        if(after != null) {
            for (Map.Entry<String, String> entry : after.entrySet())
                json.putVal(entry.getKey(), entry.getValue());
        }

        return Response.returnSuccess(json.getJson());
    }

    public String listinit()
    {
        String bstr = initflow();if(bstr.length()>0)return bstr;
        Json json   = new Json();
        json.putVal("modejs", flow.mFlowListener.onModejs() ? "1": "");
        json.putInt("isadd", flow.isAdd() ? 1: 0);
        json.putInt("isdaochu", flow.isDaochu() ? 1: 0);
        json.putInt("isdaoru", flow.isDaoru() ? 1: 0);
        json.putVal("moders", flow.moders);
        json.putVal("atypearr", flow.getAtypeArr());
        return Response.returnSuccess(json.getJson());
    }

    /**
     * 双击编辑单元格
     * */
    public String editcell()
    {
        String bstr     = initflow();if(bstr.length()>0)return bstr;
        RockMsg rockmsg = flow.SaveCelledit();
        return rockmsg.back();
    }

    /**
     * 读取数据源
     * */
    public String loadstore()
    {
        String bstr      = initflow();if(bstr.length()>0)return bstr;
        String zdid      = mRequest.get("zdid", "0");
        if(!Patter.isnumber(zdid))return Response.returnError("zdid must number");

        String data = flow.getStore(zdid);
        if(data.length()==0)data = "[]";
        Json json   = new Json();
        json.putVal("rows", data);
        return Response.returnSuccess(json.getJson());
    }

    /**
     * 录入页显示和编辑
     * */
    public String input()
    {
        String bstr = initflow();if(bstr.length()>0)return bstr;

        String mid  = mRequest.get("mid", "0");
        if(!Patter.isnumber(mid))return Response.returnError("mid must number");
        if(mRequest.USERTYPE == 0 && flow.isCompanymode() && !mRequest.COMPANYBOOL)return Response.returnError("必须单位模式下操作");

        Boolean isadd = mid.equals("0");
        flow.initData(mid);
        String data = Json.getJsonString(flow.getEditdata());

        Json json   = new Json();
        json.putVal("modejs", flow.mFlowListener.onModejs() ? "1": "");
        json.putVal("data", data);

        if(isadd && !flow.isAdd())return Response.returnError("无新增权限");

        //编辑没数据
        if(!isadd && Rock.isEmpt(data)){

        }else {
            ArrayList<Map<String, String>> farr = flow.getFieldsArr(0);
            if(isadd)farr = flow.devFieldsarr(farr);
            json.putVal("fieldsarr", Json.getListString(farr));
            json.putVal("moders", flow.moders);
            json.putVal("store", flow.getallStore(1));
            if(!isadd){
                json.putVal("subdata", flow.getSubdataAll());
                json.putVal("filedata", flow.getFiledata(1));//文件信息
            }
        }

        return Response.returnSuccess(json.getJson());
    }

    /**
     * 读取子表信息
     * */
    public String subdata()
    {
        String bstr  = initflow();if(bstr.length()>0)return bstr;
        String zdid  = mRequest.get("zdid", "0");
        if(!Patter.isnumber(zdid))return Response.returnError("zdid must number");
        return Response.returnSuccess(flow.getSubdata(zdid));
    }

    /**
     * 录入保存
     * */
    public String inputsave()
    {
        String bstr = initflow();if(bstr.length()>0)return bstr;
        String mid  = mRequest.get("mid", "0");
        if(!Patter.isnumber(mid))return Response.returnError("mid must number");
        flow.initData(mid);
        String data = flow.saveData(mid);
        return Response.returnSuccess(data);
    }

    /**
     * 单据操作菜单
     * */
    public String optmenu()
    {
        String bstr = initflow();if(bstr.length()>0)return bstr;
        String mid  = mRequest.get("mid", "0");
        if(Rock.isEmpt(mid) || !Patter.isnumber(mid))return Response.returnError("mid params error");
        flow.initData(mid);
        String data = flow.getOptmenu();

        return Response.returnSuccess(data);
    }

    /**
     * 单据操作菜单运行
     * */
    public String optmenurun()
    {
        String bstr = initflow();if(bstr.length()>0)return bstr;
        String mid  = mRequest.get("mid", "0");
        String menuid   = mRequest.get("menuid", "0");
        String lx       = mRequest.get("lx", "0");
        if(Rock.isEmpt(mid) || !Patter.isnumber(mid))return Response.returnError("mid params error");
        flow.initData(mid);
        String data     = flow.optmenuRun(menuid, lx);
        return Response.returnSuccess(data);
    }
    
    /**
     * 详情页数据
     * */
    public String xiang()
    {
    	String bstr = initflow();if(bstr.length()>0)return bstr;
        String mid  = mRequest.get("mid", "0");
        if(Rock.isEmpt(mid) || !Patter.isnumber(mid))return Response.returnError("mid params error");
        if(mRequest.USERTYPE == 0 && flow.isCompanymode() && !mRequest.COMPANYBOOL)return Response.returnError("必须单位模式下查看");

        flow.initData(mid);
    	Json json   = new Json();
        json.putVal("modejs", flow.mFlowListener.onModejs() ? "1": "");
        json.putVal("ispl", flow.mFlowListener.onisPinlun() ? "1": "");
        json.putVal("fieldsarr", flow.getFieldsArr(1));
        json.putVal("moders", flow.moders);

        json.putVal("data", flow.getXiangdata());
        json.putVal("subdata", flow.getSubdataAll());//加载子表
        json.putVal("filedata", flow.getFiledata(0));//文件信息
        json.putInt("logcount", flow.getLogcount());//操作记录统计
        json.putInt("isflow", flow.isflow);
        if(flow.isflow > 0){
            json.putVal("flowcourse", flow.getFlowCourse().getDataList()); //流程信息
            json.putVal("flowcourselog", flow.getCourseLog()); //已审批
            json.putVal("flowstatus", flow.nowStatus());
        }
        
        return Response.returnSuccess(json.getJson());
    }

    /**
     * 流程审批的
     * */
    public String check() {
        String bstr = initflow();if(bstr.length()>0)return bstr;
        String mid  = mRequest.get("mid", "0");
        if(Rock.isEmpt(mid) || !Patter.isnumber(mid))return Response.returnError("mid params error");
        flow.initData(mid);
        String sm   = Jiami.base64decode(mRequest.post("explain"));
        int zt      = mRequest.postInt("courseact");
        RockMsg rockMsg = flow.check(zt, sm);
        return rockMsg.back();
    }

    /**
     * 删除单据
     * */
    public String delete()
    {
        String bstr = initflow();if(bstr.length()>0)return bstr;
        String mid  = mRequest.get("mid", "0");
        if(Rock.isEmpt(mid) || !Patter.isnumber(mid))return Response.returnError("mid params error");
        flow.initData(mid);
        flow.delete();
        return Response.returnSuccess("ok");
    }

    /**
     * 提交评论
     * */
    public String checkping()
    {
        String bstr = initflow();if(bstr.length()>0)return bstr;
        String mid  = mRequest.get("mid", "0");
        if(Rock.isEmpt(mid) || !Patter.isnumber(mid))return Response.returnError("mid params error");
        flow.initData(mid);
        String content  = mRequest.post("content");
        String filecont = mRequest.post("filecont");
        flow.addLog("评论", content, new String[]{
                "filecont",filecont
        });
        return Response.returnSuccess();
    }

    /**
     * 调用模块接口文件里面的方法api/flow/action
     * */
    public String action()
    {
        String bstr     = initflow();if(bstr.length()>0)return bstr;
        String act      = mRequest.get("act");
        String params   = mRequest.get("params");
        String str      = flow.action(act, params);
        return Response.returnSuccess(str);
    }
}
